import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Main {

    /**
     * 传入txt路径读取txt文件
     *
     * @param txtPath
     * @return 返回读取到的内容
     */
    public static List<String> readTxt(String txtPath) {
        File file = new File(txtPath);
        if (file.isFile() && file.exists()) {
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                List<String> res = new ArrayList<>();
                String text;
                while((text = bufferedReader.readLine()) != null){
                    res.add(text);
                }
                return res;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    /**
     * 使用FileOutputStream来写入txt文件
     *
     * @param txtPath txt文件路径
     * @param content 需要写入的文本
     */
    public static void writeTxt(String txtPath, String content) {
        FileOutputStream fileOutputStream;
        File file = new File(txtPath);
        try {
            if (file.exists()) {
                //判断文件是否存在，如果不存在就新建一个txt
                file.createNewFile();
            }
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(content.getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // 读文件路径
        String txtPathRead = "src/manager.txt";
        // 写文件路径
        String txtPathWrite = "src/employee.txt";
        // 读取txt文件内容
        List<String> txtContent = readTxt(txtPathRead);
        // 存储写入文件的内容
        StringBuffer outContent = new StringBuffer();
        for (String s : txtContent) {
            // txt文件一行的内容
            String[] txtContentLine = s.split(",");
            ConcreteComponent c = new ConcreteComponent();
            ConcreteDecoratorSerial serial = new ConcreteDecoratorSerial(c, txtContentLine[0]);
            ConcreteDecoratorName name = new ConcreteDecoratorName(serial, txtContentLine[1]);
            ConcreteDecoratorBirth birth = new ConcreteDecoratorBirth(name, txtContentLine[2]);
            ConcreteDecoratorRecord record = new ConcreteDecoratorRecord(birth);
            outContent.append(record.operation());
        }
        System.out.println(outContent);
        // 写入文件内容
        writeTxt(txtPathWrite, outContent.toString());
    }


}
